home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / pdcurs21 / unix / _kbhit.c next >
C/C++ Source or Header  |  1993-06-18  |  2KB  |  85 lines

  1. #define CURSES_LIBRARY    1
  2. #include <curses.h>
  3.  
  4. #ifdef UNIX
  5. #define NOTLIB
  6. #include <defs.h>
  7. #include <term.h>
  8. #include <sys/time.h>
  9. #include <sys/types.h>
  10. #endif
  11. #undef  PDC_kbhit
  12.  
  13. #ifdef PDCDEBUG
  14. char *rcsid_PDC_kbhit = "$Header: C:\CURSES\unix\RCS\_kbhit.c 2.1 1993/06/18 20:24:04 MH Rel MH $";
  15. #endif
  16.  
  17.  
  18.  
  19. #ifdef UNIX
  20.  
  21. /*man-start*********************************************************************
  22.  
  23.   PDC_kbhit()    - Check if a character has been entered on keyboard.
  24.  
  25.   PDCurses Description:
  26.      This is a private PDCurses routine.
  27.  
  28.      Outputs character 'chr' to screen in tty fashion. If a colour
  29.      mode is active, the character is written with colour 'colour'.
  30.  
  31.   PDCurses Return Value:
  32.      This function returns OK on success and ERR on error.
  33.  
  34.   PDCurses Errors:
  35.      No errors are defined for this function.
  36.  
  37.   Portability:
  38.      PDCurses    int PDC_putc( chtype character, chtype attr );
  39.  
  40. **man-end**********************************************************************/
  41.  
  42. int PDC_kbhit(void)
  43. {
  44.     fd_set readfds, writefds, exceptfds;
  45.     struct timeval timeout;
  46.     static struct termio    otty, ntty;
  47.     int ret;
  48.  
  49. #ifdef PDCDEBUG
  50.     if (trace_on) PDC_debug("PDC_kbhit() - called\n");
  51. #endif
  52.  
  53.     /* Create proper environment for select() */
  54.     FD_ZERO( &readfds );
  55.     FD_ZERO( &writefds );
  56.     FD_ZERO( &exceptfds );
  57.     FD_SET( fileno(stdin), &readfds );
  58.  
  59.     /* We shall specify 0.5 sec as the waiting time */
  60.     timeout.tv_sec  = 0;    /*   0 seconds */
  61.     timeout.tv_usec = 500;    /* 500 microseconds */
  62.  
  63.     /* Put tty in raw mode */
  64.     ioctl(_CUR_TERM.fd, TCGETA, &otty);
  65.     ntty = otty;
  66.     ntty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
  67.     ntty.c_lflag &= ~ICANON;
  68.     ntty.c_lflag |= ISIG;
  69.     ntty.c_cflag &= ~(CSIZE|PARENB);
  70.     ntty.c_cflag |= CS8;
  71.     ntty.c_iflag &= (ICRNL|ISTRIP);
  72.     ntty.c_cc[VMIN] = 1;
  73.     ntty.c_cc[VTIME] = 1;
  74.     ioctl(_CUR_TERM.fd, TCSETAW, &ntty);
  75.  
  76.     /* Do a select */
  77.     ret = select( 1, &readfds, &writefds, &exceptfds, &timeout );
  78.  
  79.     /* Reset the tty back to its original mode */
  80.     ioctl(_CUR_TERM.fd, TCSETAW, &otty);
  81.  
  82.     return( ret );
  83. }
  84. #endif
  85.